#include "Lexicon.h"
#include "HMM_Helper.h"

Lexicon::Lexicon(void)
{

}


Lexicon::~Lexicon(void)
{
}

bool Lexicon::InitLexicon(string filename, map<string,HMM*> hmms)
{
	string line;
	vector<string> tokens;
	ifstream myfile(filename);
	
	if (myfile.is_open())
	{
		while ( myfile.good() )
		{
			getline (myfile,line);
			tokens = HMM_Helper::Split(line);

			if (tokens.size() > 0)
			{
				// add word to lexicon
				//vector<HMM*> phonemes;
				HMM* wordModel = new HMM();

				// first and last phoneme is silence
				wordModel->AddHMMSerial(hmms["sil"]);

				for (unsigned int i=1; i<tokens.size(); i++)
				{
					if (hmms[tokens[i]] != NULL)
					{
						//phonemes.push_back(hmms[tokens[i]]);
						wordModel->AddHMMSerial(hmms[tokens[i]]);

					}
					else
					{
						cout << "Phoneme " << tokens[i] << " not found!!";
						return false;
					}
				}
				
				wordModel->AddHMMSerial(hmms["sil"]);

				_lexicon[tokens[0]] = wordModel;
			}
		}
	}
	else 
	{
		return false;
	}

	return true;
}